﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Scheduler
{
    /**Class: Driver
     * @author Sara Tuohy and Hieu Nhan (Kevin)
     * @version 2.0
     * Course : ITEC 4860 Spring 2012
     * Written: March 2012
     * Driver is a temporary place holder until the login panel is deployed.
     * It provides valuable diagnostic information about the login
     * ID status - active? locked? password matched? but gives 
     * access regardless.  NOT FOR PRODUCTION USE
     * 
     * NOTE: when this panel is replaced, the two calls (setting GlobalLoggedInUserID and GlobalCompanyID)
     * (but NOT defaulting the ID to Admin) need to be copied to the new panel for subsequent panels to work correctly!
     * NOTE 2: The Driver class is now updated with the functionalities expected of a login form, but will remain as "Driver" until further notice
     */

    public partial class Driver : Form
    {
        //Scheduler.Presentation_Layer.LoginTestAgain loginTest;
        Data_Access_Layer.Login login;
      
        public Driver()
        {
            InitializeComponent();
        }

        /**
         * Method: Constructor for the Driver class
         * @author Sally Tuohy
         * Uses the LoginTestAgain to access DB information about the login ID
         */ 

        /**
        public Driver(Scheduler.Presentation_Layer.LoginTestAgain lta)
        {
            loginTest = lta;
            InitializeComponent();
            loginTest.Hide();
        }
        */

        /**
         * Method: driverManagerButton_Click
         * displays the manager main panel
         */ 
        /**
        private void driverManagerButton_Click(object sender, EventArgs e)
        {
            ManagerMain managerPanel = new ManagerMain(this);
            managerPanel.Show();       
        }*/

        /**
         * Method: driverEmployeeButton_Click
         * displays the Employee Main Panel
         */ 

        /**
        private void driverEmployeeButton_Click(object sender, EventArgs e)
        {
            //Employee Main will eventually need the user's ID and a reference to this panel ...
            Scheduler.Presentation_Layer.Employee_Main employeePanel = 
                new Scheduler.Presentation_Layer.Employee_Main(this);
            employeePanel.Show();       
        }*/

        /**Method: loginButton_Click
         * @author Hieu Nhan (Kevin)
         * Checks to see if the credentials provided match with what is in the database
         * and then takes the necessary steps as to whether the user can login
         * or will get an error message.
         */

        private void loginButton_Click(object sender, EventArgs e)
        {
            validLogin();
            invalidLogin();
        }

        /**
         * Method LogInButton_Click
         * If the ID box is blank, it assigns an ID of "Admin"
         * Otherwise, it checks the ID to see if it is an Employee or a Manager
         * In production, the appropriate panel would be displayed.  This is
         * just a test panel to allow access to whatever panels need testing.
         */ 
        /**
        private void LogInButton_Click(object sender, EventArgs e)
        {
            if (!UserIDTextBox.Text.Equals(""))
            {
                string msgString;
                login = new Data_Access_Layer.Login(UserIDTextBox.Text, PasswordTextBox.Text);

                msgString = "ID exists: " + login.doesUserExist() + "\n" +
                            "Password valid: " + login.isPasswordValid() + "\n" +
                            "Account locked: " + login.getIsAcctLocked() + "\n" +
                            "Account active: " + login.getIsAcctActive() + "\n" +
                            "Manager account: " + login.isManager() + "\n" +
                            "Employee account:" + login.isEmployee();

                MessageBox.Show(msgString,
                    "Log In Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Scheduler.Presentation_Layer.GlobalVariables.GlobalLoggedInUser = UserIDTextBox.Text;
            }

            //This method allows access JUST FOR TESTING PURPOSES.
            else Scheduler.Presentation_Layer.GlobalVariables.GlobalLoggedInUser = "Admin";
            Scheduler.Presentation_Layer.GlobalVariables.GlobalCompanyID = "GGCSWProj";
            driverManagerButton.Enabled = true;
            driverEmployeeButton.Enabled = true;
        }*/

        /**Method: keyPressed
         * @author Hieu Nhan (Kevin)
         * Dictates what methods execute when the Enter key is pressed within the 
         * userIDTextBox and passwordTextBox.
         * When pressed, it executes the validLogin and invalidLogin methods 
         */
        private void keyPressed(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                validLogin();
                invalidLogin();
            }
        }

        /**Method: invalidLogin
         * @author Hieu Nhan (Kevin)
         * Checks to see if the criteria for an invalid login are met 
         * and then supplies the matching error MessageBox
         */

        private void invalidLogin()
        {
            login = new Data_Access_Layer.Login(userIDTextBox.Text, passwordTextBox.Text);

            if (login.doesUserExist() == false || login.isPasswordValid() == false || login.getIsAcctActive() == false)
            {
                MessageBox.Show("Invalid username and/or password. Please try again.", "Invalid Login",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else if (login.getIsAcctLocked() == true)
            {
                MessageBox.Show("Your username has been locked out. Please contact your manager in order to have your login attempts renewed.", "Account Locked",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /**Method: invalidLogin
         * @author Hieu Nhan (Kevin)
         * Checks to see if the criteria for a valid login are met 
         * and then uses the supplied information to perform the login
         */

        private void validLogin()
        {
            login = new Data_Access_Layer.Login(userIDTextBox.Text, passwordTextBox.Text);
            Scheduler.Presentation_Layer.GlobalVariables.GlobalLoggedInUser = userIDTextBox.Text;
            Scheduler.Presentation_Layer.GlobalVariables.GlobalCompanyID = "GGCSWProj";

            if(login.doesUserExist() == true && login.isPasswordValid() == true && login.getIsAcctActive() == true
                && login.getIsAcctLocked() == false && login.isManager() == true)
                {
                    ManagerMain managerPanel = new ManagerMain(this);
                    managerPanel.Show();
                }

            if (login.doesUserExist() == true && login.isPasswordValid() == true && login.getIsAcctActive() == true
                && login.getIsAcctLocked() == false && login.isEmployee() == true)
            {
                Scheduler.Presentation_Layer.Employee_Main employeePanel =
                new Scheduler.Presentation_Layer.Employee_Main(this);
                employeePanel.Show();   
            }
        }

        /**Method: cancelButton_Click
         * @author Hieu Nhan (Kevin)
         * closes the form/app
         */

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
